package cn.ondraw.ragflow.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.common.core.domain.R;
import cn.ondraw.ragflow.pojo.request.CreateChatAssistantRequest;
import cn.ondraw.ragflow.pojo.request.DeleteChatAssistantRequest;
import cn.ondraw.ragflow.pojo.request.ListChatAssistantRequest;
import cn.ondraw.ragflow.pojo.request.UpdateChatAssistantRequest;
import cn.ondraw.ragflow.service.AssistantRagFlowService;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/v1/chats")
public class AssistantRagFlowController {

    private final AssistantRagFlowService assistantRagFlowService;

    /**
     * 创建聊天助手的接口
     *
     * @param request 创建聊天助手的请求对象
     * @return 创建聊天助手的响应对象
     */
    @PostMapping
    public R createChatAssistant(@RequestBody CreateChatAssistantRequest request) {
        try {
            return R.ok(assistantRagFlowService.createChatAssistant(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新聊天助手的接口
     *
     * @param chatId  要更新的聊天助手的 ID
     * @param request 更新聊天助手的请求对象
     * @return 通用响应对象
     */
    @PutMapping("/{chatId}")
    public R updateChatAssistant(@PathVariable String chatId, @RequestBody UpdateChatAssistantRequest request) {
        try {
            return R.ok(assistantRagFlowService.updateChatAssistant(chatId, request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除聊天助手的接口
     *
     * @param request 删除聊天助手的请求对象
     * @return 通用响应对象
     */
    @DeleteMapping
    public R deleteChatAssistants(@RequestBody DeleteChatAssistantRequest request) {
        try {
            return R.ok(assistantRagFlowService.deleteChatAssistants(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 列出聊天助手的接口
     *
     * @param request 列出聊天助手的请求对象
     * @return 列出聊天助手的响应对象
     */
    @GetMapping
    public R listChatAssistants(ListChatAssistantRequest request) {
        try {
            return R.ok(assistantRagFlowService.listChatAssistants(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
